package com.qianniu.napi.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.base.Strings;
import com.qianniu.napi.admin.entity.League;
import com.qianniu.napi.admin.entity.Store;
import com.qianniu.napi.admin.mapper.LeagueMapper;
import com.qianniu.napi.admin.mapper.StoreMapper;
import com.qianniu.napi.admin.service.IStoreService;
import com.qianniu.napi.admin.web.AccountController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.text.AbstractDocument;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 实体店商家地址，需要满足实体营业执照+店面+独立小程序 服务实现类
 * </p>
 *
 * @author chenxuebin
 * @since 2018-10-16
 */
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements IStoreService {

    private static final Logger logger = LoggerFactory.getLogger(StoreServiceImpl.class);

    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private LeagueMapper leagueMapper;

    @Override
    public List<Store> getNearbyStores(int pageIndex, int pageSize, double lat, double lng,
                                       long mpid, String sortby) {

        Map<String, Object> map = new HashMap<String, Object>();

        Wrapper<Store> entity = new EntityWrapper<Store>();
        if (mpid > 0) {
            entity.eq("mpid", mpid);
            map.put("mpid", mpid);
        }
        int total = storeMapper.selectCount(entity);

        if (total == 0) return null;
        int skip = (pageIndex - 1) * pageSize;
        if (Strings.isNullOrEmpty(sortby)) sortby = "distance";

        map.put("skip", skip);
        map.put("size", pageSize);
        map.put("lat", lat);
        map.put("lng", lng);
        map.put("lng", lng);
        map.put("sortby", sortby);
        String sort = "ASC";
        if (!Strings.isNullOrEmpty(sortby) && !sortby.equals("distance")) {
            sort = "desc";
        }
        map.put("sort", sort);

        return storeMapper.selectNearbyStores(map);
    }


    @Override
    public List<Store> getNearbyStores(int pageIndex, int pageSize, Long type,
                                       double lat, double lng, long mpid, String fenlei, String sortby, String sou) {

        Map<String, Object> map = new HashMap<String, Object>();

        Wrapper<Store> entity = new EntityWrapper<Store>();
        if (mpid > 0) {
            entity.eq("mpid", mpid);
            map.put("mpid", mpid);
        }
        if (Strings.isNullOrEmpty(fenlei) || fenlei.equals("is_auth")) {
            map.put("fenlei", "");
        } else if (fenlei.equals("yzstatus")) {
            map.put("fenlei", fenlei);
            entity.ge("yzstatus", 1);
        }  else {
            map.put("fenlei", fenlei);
            entity.eq(fenlei, 1);
        }
        int total = storeMapper.selectCount(entity);

        if (total == 0) return null;
        int skip = (pageIndex - 1) * pageSize;
        if (Strings.isNullOrEmpty(sortby)) sortby = "distance";

        map.put("skip", skip);
        map.put("size", pageSize);
        map.put("type", type);
        map.put("lat", lat);
        map.put("lng", lng);
        map.put("lng", lng);

        map.put("sou", Strings.isNullOrEmpty(sou) ? "" : "%" + sou.replace("%", "") + "%");
        map.put("sortby", sortby);
        String sort = "ASC";
        if (!Strings.isNullOrEmpty(sortby) && !sortby.equals("distance")) {
            sort = "desc";
        }
        map.put("sort", sort);
        logger.debug("---------------------selectNearbyStores map=" + JSON.toJSONString(map));
        return storeMapper.selectNearbyStores(map);
    }

    @Override
    public List<Store> getNearbyHYStores(int pageIndex, int pageSize, double lat, double lng,
                                         long mpid, long gid, String sortby) {

        Map<String, Object> map = new HashMap<String, Object>();

        Wrapper<Store> entity = new EntityWrapper<Store>();
        if (mpid > 0) {
            entity.eq("mpid", mpid);
            map.put("mpid", mpid);
        }
        if (gid > 0) {
            map.put("gid", gid);
        }
        int total = storeMapper.selectNearbyHYStoresCount(map);
        if (total == 0) return null;
        int skip = (pageIndex - 1) * pageSize;
        if (Strings.isNullOrEmpty(sortby)) sortby = "distance";

        map.put("skip", skip);
        map.put("size", pageSize);
        map.put("lat", lat);
        map.put("lng", lng);
        map.put("sortby", sortby);
        String sort = "ASC";
        if (!Strings.isNullOrEmpty(sortby) && !sortby.equals("distance")) {
            sort = "desc";
        }
        map.put("sort", sort);

        return storeMapper.selectNearbyHYStores(map);
    }

    @Override
    public List<Store> getHyStores(int pageIndex, int pageSize, double lat, double lng, long mpid,
                                   String sortby, Long hyid, Long renzheng) {

        List<Store> list = null;
        try {

            Map<String, Object> map = new HashMap<String, Object>();

            Wrapper<Store> entity = new EntityWrapper<Store>();
            if (mpid > 0) {
                entity.eq("mpid", mpid);
                map.put("mpid", mpid);
            }
            if (hyid > 0) {
                entity.eq("type", hyid);
                map.put("type", hyid);
            }
            if (renzheng != null && renzheng > 0) {
                map.put("renzheng", renzheng);
            }

            int total = storeMapper.selectCount(entity);
//            int total = storeMapper.selectNearbyHYStoresCount(map);
            logger.info("=========total=" + total);

            if (total == 0) return null;
            int skip = (pageIndex - 1) * pageSize;
            if (Strings.isNullOrEmpty(sortby)) sortby = "distance";

            map.put("skip", skip);
            map.put("size", pageSize);
            map.put("lat", lat);
            map.put("lng", lng);

            map.put("sortby", sortby);
            String sort = "ASC";
            if (!Strings.isNullOrEmpty(sortby) && !sortby.equals("distance")) {
                sort = "desc";
            }
            map.put("sort", sort);

            list = storeMapper.selectHYStores(map);
        } catch (Exception e) {
            logger.error("getHyStores query error", e);
        }
        return list;
    }

    @Override
    public List<Store> getNearbyLmStores(double lat, double lng, long lmid) {

        List<Store> list = null;
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("lat", lat);
            map.put("lng", lng);
            map.put("lmid", lmid);
            list = storeMapper.selectLmStores(map);
        } catch (Exception e) {
            logger.error("getHyStores query error", e);
        }
        return list;
    }

    public League getNearbyLm(double lat, double lng, long mpid) {
        Map map = new HashMap<String, Object>();
        map.put("lat", lat);
        map.put("lng", lng);
        map.put("mpid", mpid);
        return leagueMapper.getNearbyLm(map);
    }

    @Override
    public List<Store> getNearbyMFStores(double lat, double lng, long mpid, String adcode) {

        Map<String, Object> map = new HashMap<String, Object>();
        Wrapper<Store> entity = new EntityWrapper<Store>();
        if (mpid > 0) {
            entity.eq("mpid", mpid);
            map.put("mpid", mpid);
        }
        map.put("type", "meirongmeifa");
        map.put("lat", lat);
        map.put("lng", lng);
        if (!Strings.isNullOrEmpty(adcode)) {
            if (adcode.length() > 6) adcode = adcode.substring(adcode.length() - 6);
            map.put("adcode", adcode);
        }

        int total = storeMapper.selectNearbyMFStoresCount(map);
        if (total == 0) return null;

        return storeMapper.selectNearbyMFStores(map);
    }


    @Override
    public List<Store> getNearbyFavorStores(long mpid, long uid,String sou) {

        Map<String, Object> map = new HashMap<String, Object>();

        map.put("mpid", mpid);
        map.put("uid", uid);
        map.put("sou", sou);

        logger.debug("---------------------getNearbyFavorStores map=" + JSON.toJSONString(map));
        return storeMapper.getNearbyFavorStores(map);
    }
}
